package com.leyou.cart.web;

import com.leyou.cart.entity.Cart;
import com.leyou.cart.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 处理请求和响应的方法
 *
 * @version V1.0
 * @package com.leyou.cart.web
 * @description:
 * @author: pc
 * @date: 2019/7/12 17:28
 */
@RestController
public class CartController {
    @Autowired
    private ICartService cartService;

    /**
     * 添加购物车
     * */
    @PostMapping
    public ResponseEntity<Void> addItemToCart(@RequestBody Cart cart) {
        this.cartService.saveCart(cart);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 购物车批量添加商品
     * */
    @PostMapping("list")
    public ResponseEntity<Void> addCartList(@RequestBody List<Cart> carts) {
        cartService.saveCartList(carts);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 查询购物车
     * */
    @GetMapping("list")
    public ResponseEntity<List<Cart>> queryCartList() {
        List<Cart> result = cartService.queryCartList();

        return ResponseEntity.ok(result);
    }

    /**e
    * 修改购物车
    * */
    @PutMapping
    public ResponseEntity<Void> updateCart(@RequestParam("id") Long skuId,
                                           @RequestParam("num") Integer num) {
        cartService.updateCart(skuId,num);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @DeleteMapping("{skuId}")
    public ResponseEntity<Void> deleteCart(@PathVariable("skuId") Long skuId) {
        String userId = null;
        cartService.deleteCart(userId,skuId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
